package com.jzo2o.market.handler;

import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.redis.sync.SyncManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class XxlJobHandler {

    @Resource
    private SyncManager syncManager;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponService couponService;

    /**
     * 活动状态修改，
     * 1.活动进行中状态修改
     * 2.活动已失效状态修改
     * 1分钟一次
     */
    @XxlJob("updateActivityStatus")
    public void updateActivitySatus(){
        List<Activity> list = activityService.list();
        for (Activity activity : list) {
            LocalDateTime distributeStartTime = activity.getDistributeStartTime();
            activityService.lambdaUpdate().eq(Activity::getId,activity.getId()).set(LocalDateTime.now().isAfter(distributeStartTime),Activity::getStatus,2 ).update();
            activityService.lambdaUpdate().eq(Activity::getId,activity.getId()).set(LocalDateTime.now().isBefore(distributeStartTime),Activity::getStatus,3 ).update();
        }


    }

    /**
     * 已领取优惠券自动过期任务
     */
    @XxlJob("processExpireCoupon")
    public void processExpireCoupon() {
        List<Coupon> list = couponService.list();
        for (Coupon coupon : list) {
            LocalDateTime distributeStartTime = coupon.getValidityTime();
            couponService.lambdaUpdate().eq(Coupon::getId,coupon.getId()).set(LocalDateTime.now().isAfter(distributeStartTime),Coupon::getStatus,3);
        }
    }


}
